package com.rapida.shell;

import android.util.Log;

import java.io.IOException;
import java.io.OutputStreamWriter;

/**
 * Created by mm on 2014/12/31.
 */
public class Shell {
	private static final String TAG = "Shell";

	public static void executeShellCommand(String shellCommand) {
		java.lang.Process proc = null;
		OutputStreamWriter osw = null;

		try {
			proc = Runtime.getRuntime().exec(shellCommand);
			osw = new OutputStreamWriter(proc.getOutputStream());
			if (null != osw) {
				osw.write(shellCommand);
				osw.write("\n");
				osw.write("exit\n");
				osw.flush();
				osw.close();
			}
		} catch (IOException ex) {
			Log.e(TAG, "execCommandLine() IO Exception");
			return;
		} finally {
			if (null != osw) {
				try {
					osw.close();
				} catch (IOException e) {
				}
			}
		}

		try {
			proc.waitFor();
		} catch (InterruptedException e) {
		}

		if (proc.exitValue() != 0) {
			Log.e(TAG, "execCommandLine() Err exit code: " + proc.exitValue());
		}
	}
}
